
var newer = newer || {};
newer.gd = newer.gd || {};

newer.PostShareDropDown = function(o) {
	this.result = new Object();
	this.result.people = "";
	this.result.circle = "";
	this.cp = o.cp;
	
	this.circlesList = new Array();
	this.legal = true;
	
	this.pid = newer.gd.user.id;
	this.custom = false;
	this.preset = o.preset;
	//this.presetForce = true;
	this.items = new Object();
	
	
	this.root = o.root;
	this.init();	
}

newer.PostShareDropDown.prototype = {
	
	init: function() {
		var self = this;

		$("#"+this.root).html('<div id="post-create-share-option-button" class="link-like-element post-create-share-option"><span id="pcso-button-name"></span><img style="vertical-align:middle;" width="20px" src="/public/images/arrow_return_right_down.png"/></div>');
		
		$("#post-create-share-option-button").append('<div id="post-share-drop-down-list"></div>');
		
		$("#post-share-drop-down-list").append('<div id="post-share-drop-down-all" class="post-share-drop-down-item"></div>');
		this.items["all"] = new newer.PostShareDropDownItem({label:"Friends",id:"post-share-drop-down-all",psdd:self});
		
		$.ajax({
			url: "/Circles/getCirclesByUserId?pid="+this.pid, 
			async: false,
			success: function(circles){
				if(!circles.error) {
					$.each(circles, function(index, circle) {
						$("#post-share-drop-down-list").append('<div id="post-share-drop-down-circles-'+circle.id+'" class="post-share-drop-down-item"></div>');
						self.items["circle-"+circle.id] = new newer.PostShareDropDownItem({label:circle.name,id:"post-share-drop-down-circles-"+circle.id,psdd:self,isCircle:true,cid:circle.id});
						self.circlesList.push(self.items["circle-"+circle.id]);
					});
				}
			}
		});
		
		$("#post-create-share-option-button").on("mouseenter", function(){
			$("#post-share-drop-down-list").show();//.slideDown("fast");
		})
		
		$("#post-share-drop-down-list").on("mouseleave", function(){
			$("#post-share-drop-down-list").hide();//.slideUp("fast");
		})
		
		this.setDefault();
		this.setButtonName();
	},
	
	setButtonName: function() {
		var i = 0;
		var icircle = 0;
		var inoncircle = 0;
		var res = "";
		for(var key in this.items) {
			if(this.items[key].checked) {
				res = this.items[key].label;
				i++;
				this.items[key].isCircle?icircle++:inoncircle++;
			}
		}
		if(i>1) {
			res = "Custom";
		}
		if(inoncircle==0 && icircle>0) {
			res = icircle+" Circle" + (icircle==1?"":"s");
		}
		$("#pcso-button-name").html(res);
	}, 

	setDefault: function() {
		var self = this;
		this.items[this.preset].beClicked();
	}, 

	generateRes: function() {
		this.legal = false;
		
		this.result.people = "";
		if(this.items["all"].checked) {
			this.result.people += "all";
			this.legal = true;
		} else {
			this.result.people += "";
		}
		
		
		this.result.circle = "";
		var first = true;
		for(var i in this.circlesList) {
			var c = this.circlesList[i];
			if(c.checked){
				this.result.circle += ((first?"":",")+c.cid);
				this.legal = true;
				first = false;
			}
		}
	}
};

newer.PostShareDropDownItem = function(o) {
	this.label = o.label;
	this.id = o.id;
	this.checked = false;
	this.forceChecked = false;
	this.isCircle = false || o.isCircle;
	this.psdd = o.psdd;
	this.cid = o.cid;

	this.init();
	
	
};

newer.PostShareDropDownItem.prototype = {
	init: function() {
		$("#"+this.id).html(this.label + '<img class="post-share-drop-item-check" src="/public/images/check.png" width="15px" style="display:none;"/>');
		$("#"+this.id).on("click", {self:this}, function(event) {
			var self = event.data.self;
			self.beClicked();
		});
	},
	
	select: function() {
		$("#"+this.id+" img").show();
		this.checked = true;
	}, 
	
	cancel: function() {
		if(!this.forceChecked) {
			$("#"+this.id+" img").hide();
			this.checked = false;
		}
	},
	
	beClicked: function() {
		var self = this;
		if(self.checked) {
			self.cancel();
		} else {
			self.select();
		}
		self.psdd.setButtonName();
		self.psdd.generateRes();
		$.debug("people: " + self.psdd.result.people);
		$.debug("circle: " + self.psdd.result.circle);
		
//		if(!self.psdd.legal && $("#post-create-share-div").hasClass("post-create-share-div-enable")) {
//			$("#post-create-share-div").removeClass("post-create-share-div-enable").addClass("post-create-share-div-disable");
//		} else if(self.psdd.legal && $("#post-create-share-div").hasClass("post-create-share-div-disable")) {
//			$("#post-create-share-div").removeClass("post-create-share-div-disable").addClass("post-create-share-div-enable");
//		}
		
	}
	
	
};